function iTunesWebController() {
  this.url = 'itunes.php';
}


iTunesWebController.PlaylistSearchField = {
  All: 0,
  Visible: 1,
  Aritists: 2,
  Album: 3,
  Composers: 4,
  SongNames: 5
};


iTunesWebController.prototype.createXMLHttpRequest_ = function() {
  try { return new XMLHttpRequest(); } catch(e) {}
  try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
  return null;
}


iTunesWebController.prototype.getUrl_ = function(cmd, gargs) {
  var url = this.url + '?cmd=' + cmd;
  if (gargs) {
    for (var i = 0; i < gargs.length; i++) {
      url = url + '&arg' + i + '=' + encodeURIComponent(gargs[i]);
    }
  }
  return url;
}


iTunesWebController.prototype.get_ = function(cmd, gargs, pargs, 
    callback, errorCallback) {
  var request = this.createXMLHttpRequest_();
  var url = this.getUrl_(cmd, gargs);
  var method = 'GET';
  if (pargs) {
    method = 'POST';
  }
  request.open(method, url, true);
  request.onreadystatechange = function() {
    if (request.readyState != 4) {
      return;
    }
    if (request.status != 200)  {
      // Handle error, e.g. Display error message on page
      if (errorCallback) {
        errorCallback();
      }
      return;
    }
    var result = '';
    if (request.responseText != '') {
      result = eval('(' + request.responseText + ')');
    }
    if (callback) {
      callback(result);
    }
  };
  request.send(pargs);
}


iTunesWebController.prototype.getCurrentTrack = function(callback, errorCallback) {
  this.get_('getcurrenttrack', null, null, callback, errorCallback);
}


iTunesWebController.prototype.getPlayerPosition = 
    function(callback, errorCallback) {
  this.get_('getplayerposition', null, null, callback, errorCallback);
}


iTunesWebController.prototype.getPlayerState = function(callback, errorCallback) {
  this.get_('getplayerstate', null, null, callback, errorCallback);
}


iTunesWebController.prototype.getPlaylist = function(sourceId, playlistId,
    callback, errorCallback) {
  var gargs = [ sourceId, playlistId ];
  this.get_('getplaylist', gargs, null, callback, errorCallback);
}


iTunesWebController.prototype.getPlaylists = function(callback, errorCallback) {
  this.get_('getplaylists', null, null, callback, errorCallback);
}


iTunesWebController.prototype.getTrack = function(sourceId, playlistId, trackId, 
    databaseId, callback, errorCallback) {
  var gargs = [ sourceId, playlistId, trackId, databaseId ];
  this.get_('gettrack', gargs, null, callback, errorCallback);
}


iTunesWebController.prototype.getTracksInPlaylist = function(sourceId, playlistId,
    callback, errorCallback) {
  var gargs = [ sourceId, playlistId ];
  this.get_('gettracksinplaylist', gargs, null, callback, errorCallback);
}


iTunesWebController.prototype.getVolume = function(callback, errorCallback) {
  this.get_('getvolume', null, null, callback, errorCallback);
}


iTunesWebController.prototype.isMute = function(callback, errorCallback) {
  this.get_('ismute', null, null, callback, errorCallback);
}


iTunesWebController.prototype.nextTrack = function(callback, errorCallback) {
  this.get_('nexttrack', null, null, callback, errorCallback);
}


iTunesWebController.prototype.pause = function(callback, errorCallback) {
  this.get_('pause', null, null, callback, errorCallback);
}


iTunesWebController.prototype.play = function(callback, errorCallback) {
  this.get_('play', null, null, callback, errorCallback);
}


iTunesWebController.prototype.playPause = function(callback, errorCallback) {
  this.get_('playpause', null, null, callback, errorCallback);
}


iTunesWebController.prototype.previousTrack = function(callback, errorCallback) {
  this.get_('previoustrack', null, null, callback, errorCallback);
}


iTunesWebController.prototype.search = function(term, field, callback, errorCallback) {
  var gargs = [ term, field ];
  this.get_('search', gargs, null, callback, errorCallback);
}


iTunesWebController.prototype.setMute = function(muteValue, callback, errorCallback) {
  this.get_('setmute', [ muteValue ], null, callback, errorCallback);
}


iTunesWebController.prototype.stop = function(callback, errorCallback) {
  this.get_('stop', null, null, callback, errorCallback);
}

